var express = require('express');
var router = express.Router();
const facService = require('../service/facService');

//获取全部
router.get('/all', async function (req, res) {
    let data = await facService.findAll();
    res.send({
        data
    })
})
//双向id查询
router.get('/films/:filmId/cinemas/:cinemaId', async function (req, res) {
    let { filmId, cinemaId } = req.params;
    let data = await facService.findFacAll(filmId, cinemaId);
    console.log(filmId, cinemaId);
    console.log("16", data);
    res.send({
        data
    })
})

//查询
router.get('/', async function (req, res) {
    let { type, id, current, pageSize } = req.query;
    let searchObj = {};
    if (type) {
        searchObj[type] = id;
    }
    let data = await facService.find(searchObj, {
        current: current || 1,
        pageSize: pageSize || 10
    })
    console.log('fac', data);
    res.send({
        data
    })
})

//添加
router.post('/', async function (req, res) {
    let { films, cinemas, schedule } = req.body;
    schedule = JSON.parse(schedule);
    let data = await facService.insert({ films, cinemas, schedule })
    res.send({
        data
    })
})

//删除
router.delete('/:id', async function (req, res) {
    let { id } = req.params;
    console.log(req.params);
    let result = await facService.delete(id);
    res.send({
        result
    })
})

//id查询
router.get('/:id', async function (req, res) {
    let { id } = req.params;
    let result = await facService.findById(id);
    res.send({
        result
    })
})

//id修改
router.put('/:id', async function (req, res) {
    let { id } = req.params;
    console.log(id);
    let { films, cinemas, schedule } = req.body;
    schedule = JSON.parse(schedule);
    let result = await facService.update(id, { films, cinemas, schedule });
    res.send({
        result
    })
})

module.exports = router;